﻿using System;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYCodeBase.WinForm;
using YRevitJig.Jig;

namespace YRevitJig
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class JigLine : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			ViewAssist.ViewValidating(activeUIDocument.Document, activeUIDocument.ActiveView);
			Process currentProcess = Process.GetCurrentProcess();
			IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
			WindowHandle owner = new WindowHandle(mainWindowHandle);
			LineJigForm lineJigForm = new LineJigForm();
			lineJigForm.Show(owner);
			XLineJig xlineJig = new XLineJig(activeUIDocument, lineJigForm);
			XYZ xyz = xlineJig.PickStartPoint("请确定直线起点：");
			bool flag = xyz == null;
			Result result;
			if (flag)
			{
				xlineJig.Dispose();
				lineJigForm.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			else
			{
				xlineJig.SetPromptState("", false, true);
				xlineJig.DrawSize = true;
				for (;;)
				{
					XYZ xyz2 = xlineJig.PickEndPoint(xyz, "请确定风管终点：");
					bool flag2 = xyz2 == null;
					if (flag2)
					{
						break;
					}
					Transaction transaction = new Transaction(activeUIDocument.Document);
					try
					{
						transaction.Start("CreateCurve");
						Line line = Line.CreateBound(xyz, xyz2);
						activeUIDocument.Document.Create.NewDetailCurve(activeUIDocument.ActiveView, line);
						transaction.Commit();
					}
					catch (Exception ex)
					{
						transaction.RollBack();
					}
					xyz = xyz2;
				}
				xlineJig.Dispose();
				lineJigForm.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}
	}
}
